//
//  GoodsInformationViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/17.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "GoodsInformationViewController.h"

#import "GoodsLogisticsFirstTableViewCell.h"
#import "GoodsLogisticsTableViewCell.h"


static NSString *goodsInformationFirstCellID = @"goodsInformationFirstCellID";
static NSString *goodsInformationCellID = @"goodsInformationCellID";

@interface GoodsInformationViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *informationTableView;

// 商品图
@property (strong, nonatomic) UIImageView *goodsIV;
// 商品描述
@property (strong, nonatomic) UILabel *desL;
// 发货状态
@property (strong, nonatomic) UILabel *goodsStatusL;
// 发货状态描述
@property (strong, nonatomic) UILabel *goodsStatusDesL;

// 快递logo
@property (strong, nonatomic) UIImageView *logisticsLogoIV;
// 快递名称
@property (strong, nonatomic) UILabel *logisticsL;
// 快递编号
@property (strong, nonatomic) UILabel *logisticsNumL;
// 官方电话
@property (strong, nonatomic) UILabel *logisticsPhoneNumL;


@property (strong, nonatomic) NSMutableArray *arrayM;

@end

@implementation GoodsInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = WhiteColor;
    self.view.backgroundColor = Colordddddd;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"物流信息";
    
    [self createUI];
    [self createHeader];
    

}

- (void)createUI {

    [self.view addSubview:self.informationTableView];
    
    __weak typeof(self) weakSelf = self;
    [self.informationTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        make.top.mas_equalTo(weakSelf.view.mas_top);
    }];
    
}

#pragma mark -头部视图
- (void)createHeader {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = Colordddddd;
    headerView.frame = CGRectMake(0, 0, WIDTH, jisuan(716));
    
    UIImageView *firstShadowIV = [[UIImageView alloc] init];
    firstShadowIV.contentMode = UIViewContentModeScaleAspectFit;
    firstShadowIV.image = [UIImage imageNamed:@"goodsInformationTopShadowBG"];
    [headerView addSubview:firstShadowIV];
    [firstShadowIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(headerView.mas_top).with.offset(jisuan(10));
        make.centerX.mas_equalTo(headerView.mas_centerX);
        make.width.mas_equalTo(jisuan(690));
        make.height.mas_equalTo(jisuan(446));

    }];
    
    UIImageView *goodsIV = [[UIImageView alloc] init];
    self.goodsIV = goodsIV;
    goodsIV.contentMode = UIViewContentModeScaleAspectFit;
    goodsIV.image = [UIImage imageNamed:@"goodsBG"];
    [headerView addSubview:goodsIV];
    [goodsIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(headerView.mas_centerX);
        make.top.mas_equalTo(firstShadowIV.mas_top).with.offset(jisuan(60));
        make.width.mas_equalTo(jisuan(120));
        make.height.mas_equalTo(jisuan(120));
    }];
    
    UILabel *desL = [[UILabel alloc] init];
    self.desL = desL;
    desL.text = @"小埋家的八二年雪碧非常好喝好喝包装漂亮然后没啊…";
    desL.font = Font(24);
    desL.textAlignment = NSTextAlignmentCenter;
    desL.textColor = Color333333;
    [headerView addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(headerView.mas_centerX);
        make.top.mas_equalTo(goodsIV.mas_bottom).with.offset(jisuan(60));
        make.left.mas_equalTo(firstShadowIV.mas_left).with.offset(jisuan(50));
        make.right.mas_equalTo(firstShadowIV.mas_right).with.offset(-jisuan(50));
        
    }];
    
    UIImageView *redIV = [[UIImageView alloc] init];
    redIV.contentMode = UIViewContentModeScaleAspectFit;
    redIV.image = [UIImage imageNamed:@"goodsInformationTopRedBG"];
    [headerView addSubview:redIV];
    [redIV mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.left.mas_equalTo(firstShadowIV.mas_left);
        make.right.mas_equalTo(firstShadowIV.mas_right);
        make.bottom.mas_equalTo(firstShadowIV.mas_bottom);
        make.height.mas_equalTo(jisuan(132));
        
    }];

    UILabel *goodsStatusL = [[UILabel alloc] init];
    self.goodsStatusL = goodsStatusL;
    goodsStatusL.textColor = WhiteColor;
    goodsStatusL.text = @"已发货";
    goodsStatusL.font = Font(24);
    goodsStatusL.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:goodsStatusL];
    [goodsStatusL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(redIV.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(redIV.mas_top).with.offset(jisuan(30));
        
    }];
    
    UILabel *goodsStatusDesL = [[UILabel alloc] init];
    self.goodsStatusDesL = goodsStatusDesL;
    goodsStatusDesL.textColor = WhiteColor;
    goodsStatusDesL.text = @"包裹离你越来越近了";
    goodsStatusDesL.font = Font(20);
    goodsStatusDesL.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:goodsStatusDesL];
    [goodsStatusDesL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(redIV.mas_left).with.offset(jisuan(40));
        make.bottom.mas_equalTo(redIV.mas_bottom).with.offset(-jisuan(30));
        
    }];
    
    UIImageView *logisticsIV = [[UIImageView alloc] init];
    logisticsIV.contentMode = UIViewContentModeScaleAspectFit;
    logisticsIV.image = [UIImage imageNamed:@"goodsInformationMiddleBG"];
    [headerView addSubview:logisticsIV];
    [logisticsIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(headerView.mas_centerX);
        make.bottom.mas_equalTo(headerView.mas_bottom).with.offset(-jisuan(40));
        make.width.mas_equalTo(jisuan(690));
        make.height.mas_equalTo(jisuan(190));
    }];
    
    UIImageView *logisticsLogoIV = [[UIImageView alloc] init];
    self.logisticsLogoIV = logisticsLogoIV;
    logisticsLogoIV.contentMode = UIViewContentModeScaleAspectFit;
    logisticsLogoIV.image = [UIImage imageNamed:@"deliverSmallBG"];
    [headerView addSubview:logisticsLogoIV];
    [logisticsLogoIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(logisticsIV.mas_centerY);
        make.left.mas_equalTo(logisticsIV.mas_left).with.offset(jisuan(40));
        make.width.mas_equalTo(jisuan(80));
        make.height.mas_equalTo(jisuan(80));
    }];

    UILabel *logisticsL = [[UILabel alloc] init];
    self.logisticsL = logisticsL;
    logisticsL.textColor = Color333333;
    logisticsL.text = @"顺丰速递";
    logisticsL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(24)];
    logisticsL.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:logisticsL];
    [logisticsL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(logisticsLogoIV.mas_right).with.offset(jisuan(40));
        make.top.mas_equalTo(logisticsIV.mas_top).with.offset(jisuan(40));
        
    }];
    
    UILabel *logisticsNumL = [[UILabel alloc] init];
    self.logisticsNumL = logisticsNumL;
    logisticsNumL.textColor = Color333333;
    logisticsNumL.text = @"运单号：87932475239879";
    logisticsNumL.font = Font(20);
    logisticsNumL.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:logisticsNumL];
    [logisticsNumL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(logisticsLogoIV.mas_right).with.offset(jisuan(40));
        make.centerY.mas_equalTo(logisticsIV.mas_centerY);
        
    }];
    
    UILabel *logisticsPhoneNumL = [[UILabel alloc] init];
    self.logisticsPhoneNumL = logisticsPhoneNumL;
    logisticsPhoneNumL.textColor = Color333333;
    logisticsPhoneNumL.text = @"官方电话：10086";
    logisticsPhoneNumL.font = Font(20);
    logisticsPhoneNumL.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:logisticsPhoneNumL];
    [logisticsPhoneNumL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(logisticsIV.mas_bottom).with.offset(-jisuan(50));
        make.left.mas_equalTo(logisticsLogoIV.mas_right).with.offset(jisuan(40));
        
    }];
    
    self.informationTableView.tableHeaderView = headerView;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        return jisuan(90);
        
    }else {
        
        return jisuan(120);
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *v = [[UIView alloc] init];
    v.backgroundColor = Colordddddd;
    
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return jisuan(40);
}

#pragma mark -delegate \ datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrayM.count + 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GoodsLogisticsFirstTableViewCell *cell1 = [[GoodsLogisticsFirstTableViewCell alloc] init];
    
    GoodsLogisticsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:goodsInformationCellID];
    
    if (!cell) {
        
        cell = [[GoodsLogisticsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsInformationCellID];
    
    }
    
    if (!cell1) {
        
        cell1 = [[GoodsLogisticsFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsInformationCellID];
        
    }
    
    if (indexPath.row == 0) {
        
        return cell1;
        
    }else {
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
//        cell.tableView = tableView;
        
        cell.type = indexPath.row;
        cell.dict = self.arrayM[indexPath.row - 1];
        
        if (self.arrayM.count == indexPath.row) {
            
            cell.bottomLineView.hidden = YES;
            cell.dashedView.hidden = YES;
        }

        return cell;
        
    }

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat cornerRadius = 8.f;
    cell.backgroundColor = UIColor.clearColor;
    
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init];
    CGMutablePathRef pathRef = CGPathCreateMutable();

    CGRect bounds = cell.bounds;
    
    NSInteger numberOfRows = 0;
    if (self.arrayM.count > 0) {
        
        numberOfRows = self.arrayM.count + 1;

    }
    
    if (indexPath.row == 0 && numberOfRows == 1) {
        CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
    }else if (indexPath.row == 0) {
        // 初始起点为cell的左下角坐标
        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
        
    } else if (indexPath.row == numberOfRows -1) {
        // 初始起点为cell的左上角坐标
        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
    } else {
        CGPathAddRect(pathRef, nil, bounds);
        
    }
    
    layer.path = pathRef;
    backgroundLayer.path = pathRef;
    CFRelease(pathRef);
//    layer.fillColor = Color1f89ff.CGColor;
    layer.fillColor = WhiteColor.CGColor;

    UIView *roundView = [[UIView alloc] initWithFrame:bounds];
    [roundView.layer insertSublayer:layer atIndex:0];
//    roundView.backgroundColor = UIColor.clearColor;
//    roundView.backgroundColor = Color1f89ff;
    cell.backgroundView = roundView;

//    
//    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
//
//    backgroundLayer.fillColor = WhiteColor.CGColor;
//    [selectedBackgroundView.layer insertSublayer:backgroundLayer below:cell.layer];
//    selectedBackgroundView.backgroundColor = UIColor.clearColor;
//    cell.selectedBackgroundView = selectedBackgroundView;
//    
//
    
}

#pragma mark -lazy

- (UITableView *)informationTableView {
    
    if (!_informationTableView) {
        
        _informationTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _informationTableView.backgroundColor = Colordddddd;

        _informationTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _informationTableView.delegate = self;
        _informationTableView.dataSource = self;
        self.informationTableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"noData"
                                                                      titleStr:@"暂无数据"
                                                                     detailStr:@""];
        _informationTableView.showsVerticalScrollIndicator = NO;
        _informationTableView.showsHorizontalScrollIndicator = NO;
        
        _informationTableView.estimatedRowHeight = 0;
        _informationTableView.estimatedSectionFooterHeight = 0;
        _informationTableView.estimatedSectionHeaderHeight = 0;
        
        if (@available(iOS 11.0, *)) {
            
            _informationTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _informationTableView;
}

- (NSMutableArray *)arrayM {
    
    if (!_arrayM) {
        
        _arrayM = [NSMutableArray array];
        
 
        NSDictionary *last = @{@"date" : @"", @"time" : @"", @"image" : @"goodsLogisticsBG", @"status" : @"", @"address" : @"[收货地址] 大红门街道   大红门西路2号建新园六 里2号楼2单元2333室"};
        
        NSDictionary *one = @{@"date" : @"07-04", @"time" : @"16:31", @"image" : @"goodsLogisticsBG", @"status" : @"派送中", @"address" : @"北京市[北京大红门分部]，[小埋]正在派件"};
        NSDictionary *two = @{@"date" : @"07-04", @"time" : @"16:31", @"image" : @"goodsLogisticsBG", @"status" : @"运输中", @"address" : @"北京市[北京大红门分部]"};
        NSDictionary *three = @{@"date" : @"07-04", @"time" : @"16:31", @"image" : @"goodsLogisticsBG", @"status" : @"", @"address" : @"到北京市[北京转运中心]"};
    
        [_arrayM addObject:last];
        [_arrayM addObject:one];
        [_arrayM addObject:two];
        [_arrayM addObject:three];
        
    }
    
    return _arrayM;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
//    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
//    [super viewWillDisappear:animated];
//    
//    self.navigationController.navigationBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
